<?php
/**
 * TESTECLASS
 * Description number 1
 * description 2
 * description 3
 * @version
 * @author Greg
 * @date 23.03.2008
 */
class testeClass extends DM{
	/**
	 * MyID
	 *
	 * @var teste_id - INT
	 */
	var $teste_id = "";
	/**
	 * Nome
	 *
	 * @var nome - VARCHAR
	 */
	var $nome = "";
	/**
	 * Email
	 *
	 * @var email - VARCHAR
	 */
	var $email = "";
	/**
	 * Apelido
	 *
	 * @var apelido - VARCHAR
	 */
	var $apelido = "";
	/**
	 * Habilitado
	 *
	 * @var habilitado - ONOFF
	 */
	var $habilitado = "";
	/**
	 * Sexo
	 *
	 * @var sexo - ENUM
	 */
	var $sexo = "";
	/**
	 * updated_on
	 *
	 * @var updated_on - TIMESTAMP
	 */
	var $updated_on = "";
	/**
	 * created_on
	 *
	 * @var created_on - TIMESTAMP
	 */
	var $created_on = "";

	/**
	 * Debugging status
	 *
	 * @var debugging boolean
	 */
	var $debugging=true;

	/**
	 * The ID list
	 *
	 * @var lista array
	 */
	var $lista = "";


	/**
	 * Debug method - should really be in DM
	 * When $debugging is set to TRUE, it displays the log of actions taken by the class.
	 *
	 * @param boolean $result - what do you want returned
	 * @param string $operation - the action that will be displayed in the debug
	 * @return boolean
	 */
	function debug($result,$operation=""){
		if ($result){
			if ($this->debugging){
				echo "<div style='font-size:10px;font-family:arial,sans;color:#FFFFFF;background-color:#008800'>OK: ";
				echo strtoupper($this->db_type)."($operation) - [No errors computed] ";
				echo "</div>";
			}
			return true;
		}else{
			if ($this->debugging){
				echo "<div style='font-family:arial,sans;color:#FFFFFF;background-color:#ff0000;font-size:10px;'>ERROR: ";
				echo strtoupper($this->db_type)."($operation) - [".$this->db->errDesc."] ".$this->db->errDesc;
				echo "</div>";
			}
			return false;
		}

	}

	/**
	 * CONSTRUCTOR METHOD - Initializes the object
	 *
	 */
	function testeClass(){
		//LOAD THE DATABSE CLASS
		$this->loadClass('database','database');

		//INITIALIZE THE VARIABLES
		$this->teste_id = "";
		$this->nome = "";
		$this->email = "";
		$this->apelido = "";
		$this->habilitado = "";
		$this->sexo = "";
		$this->updated_on = "";
		$this->created_on = "";
		$this->lista = array();

	}

	/**
	 * Fetch all the IDs - Optional where variable allows you to set conditions
	 *
	 * @param String $where - when set, allows you to add SQL to the select
	 * @return boolean
	 */
	function getList($where=1){

		//SQL STRING
		$sql="SELECT teste_id FROM teste_empresa WHERE $where";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->fetch($sql)){
				$this->lista=$db->resultArray;
				return $this->debug(true,'get_list');
			}else{
				$this->errorReport("Error in the select",$sql);
				return $this->debug(false,"get_List - $sql");
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return $this->debug(false,'get_list');
		}
	}

	/**
	 * Allows you to limit the results of your query
	 *
	 * @param string $where - Where clause when applicable
	 * @param int $start - row number to start on
	 * @param int $limit - limit of rows to return
	 * @return boolean
	 */
	function getPage($where='',$start=0,$limit=30){

		//FIX WHERE
		if ($where!=''){
			$wg='WHERE '.$where;
		}else{
			$wg='';
		}
		//SQL STRING
		$sql="SELECT teste_id FROM teste_empresa $wg LIMIT $start, $limit ";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->fetch($sql)){
				$this->lista=$db->resultArray;
				return $this->debug(true,'get_list');
			}else{
				$this->errorReport("Error in the select",$sql);
				return $this->debug(false,"get_List - $sql");
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return $this->debug(false,'get_list::DB::connect');
		}
	}

	/**
	 * fill the current object.
	 *
	 * @return boolean
	 */
	function getById(){
		if ($this->teste_id!=""){
			$sql = "select * from teste_empresa where teste_id='$this->teste_id'";
			$db= new database();
			$db->debugging=$this->debugging;
			if ($db->connect()){
				if ($db->fetch($sql)){
					if (count($db->resultArray)==1){
						$this->teste_id = $db->resultArray[0]["teste_id"];
						$this->nome = $db->resultArray[0]["nome"];
						$this->email = $db->resultArray[0]["email"];
						$this->apelido = $db->resultArray[0]["apelido"];
						$this->habilitado = $db->resultArray[0]["habilitado"];
						$this->sexo = $db->resultArray[0]["sexo"];
						$this->updated_on = $db->resultArray[0]["updated_on"];
						$this->created_on = $db->resultArray[0]["created_on"];
						return $this->debug(true,'getById');
					}else{
						$this->errorReport("Error too many results.",$sql);
						return $this->debug(false,'getById:expected 1 result');
					}
				}else{
					$this->errorReport("Error in the select",$sql);
					return $this->debug(false,'getById::DB:Select problem [$sql]');
				}
			}else{
				$this->errorReport("Could not conncet to the database",$db);
				return $this->debug(false,'getById::DB:connect');
			}
		}
	}
	/**
	 * make the insert call
	 *
	 * @access private
	 * @return boolean
	 */
	function _insert(){
		//insert
		$sql = "";
		$sql .= "INSERT INTO teste_empresa(";
		$sql .= " teste_id";
		$sql .= ", nome";
		$sql .= ", email";
		$sql .= ", apelido";
		$sql .= ", habilitado";
		$sql .= ", sexo";
		$sql .= ", updated_on";
		$sql .= ", created_on";
		$sql .= ") VALUES (";
		$sql .= " ''";
		$sql .= ", '$this->nome'";
		$sql .= ", '$this->email'";
		$sql .= ", '$this->apelido'";
		$sql .= ", '$this->habilitado'";
		$sql .= ", '$this->sexo'";
		$sql .= ", NOW()";
		$sql .= ", NOW()";
		$sql .= ")";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->execute($sql)){
				$this->teste_id=$db->lastInsertID();
				return $this->debug(true,'_insert');
			}else{
				$this->errorReport("Error in the insert",$sql);
				return $this->debug(false,'_insert');
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return $this->debug(false,'_insert::DB:connect');
		}
	}
	/**
	 * make the update call
	 *
	 * @access private
	 * @return boolean
	 */
	function _update(){
		//update
		$sql = "";
		$sql .= "UPDATE teste_empresa SET";
		$sql .= " nome";
		$sql .= " = '$this->nome'";
		$sql .= ", email";
		$sql .= " = '$this->email'";
		$sql .= ", apelido";
		$sql .= " = '$this->apelido'";
		$sql .= ", habilitado";
		$sql .= " = '$this->habilitado'";
		$sql .= ", sexo";
		$sql .= " = '$this->sexo'";
		$sql .= ", updated_on";
		$sql .= " = NOW()";
		$sql .= " where ";
		$sql .= " teste_id = '".$this->teste_id."'";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->execute($sql)){
				return $this->debug(true,'_update');
			}else{
				$this->errorReport("Error in the select",$sql);
				return $this->debug(false,'_update');
			}
		}else{
			$this->errorReport("Could not conncet to the database",$db);
			return $this->debug(false,'_update::DB:connect');
		}
	}
	/**
	 * SAVE THE OBJECT - if an ID is set, it update, otherwise inserts
	 *
	 * @return boolean
	 */
	function save(){
		if ($this->teste_id==""){
			return $this->_insert();
		}else{
			return $this->_update();
		}
	}
	/**
	 * Delete the currently selected object
	 *
	 * @return boolean
	 */
	function delete(){
		$sql="DELETE FROM teste_empresa WHERE teste_id='$this->teste_id'";
		$db= new database();
		$db->debugging=$this->debugging;
		if ($db->connect()){
			if ($db->execute($sql)){
				return $this->debug(true,'delete');
			}else{
				$this->errorReport("Error in the SQL:<BR>",$sql);
				return $this->debug(false,'delete');
			}
		}else{
			$this->errorReport("Error in the Connect");
			return $this->debug(false,'delete::DB:connect');
		}
	}


}
?>
